<?php

namespace app\module\admin\controller;

use app\BaseController;
use app\model\AuthGroupAccess;
use app\model\DepartmentUser;
use app\model\RoleUser;
use app\Model\User as UserModel;
use think\db\Query;
use think\facade\Db;

class User extends BaseController
{
    public function getUserList(): \think\Response
    {
        $departmentId = $this->request->get('department_id');
        $page = $this->request->get('page', 1);
        $size = $this->request->get('size', 10);
        $status = $this->request->get('status', 1);
        $name = $this->request->get('name');
        $department = DepartmentUser::where('status', 1)->find();
        if (empty($department))
            return $this->error('部门信息不存在');
        $query = DepartmentUser::where('department_id', $departmentId)->withJoin([
            'user' => function (Query $q) use ($name, $status) {
                $q->where('user.status', $status);
                if(!empty($name)){
                    $q->where('user.name',$name); 
                }
            }]);;

        $list = $query->page($page, $size);
        $list->each(function (&$item) use ($department) {
            $item->department_name = $department['name'];
        });
        return $this->success($list);
    }

    public function getUserInfo()
    {
        $userId = $this->request->get('user_id');
        if (empty($userId))
            return $this->error('用户id不能为空');
        $userInfo = UserModel:: alias('a')->with([
            'departments', 'roles'
        ])->where('a.status', 1)->withoutField('password,account')->find($userId);

        return $this->success($userInfo);
    }

    public function save()
    {
        $userId = $this->request->post('user_id');
        $user = UserModel::find($userId);
        if (empty($user))
            $user->save($this->request->post());
        else
            $user->save($this->request->except('user_id'));
        return $this->success();
    }

    /**
     * 新增或者修改
     */
    public function saveDepartment(): \think\Response
    {

        $postData = $this->request->post();
        if (empty($postData['user_id'])) return $this->error('用户id不能为空');
        if (empty($postData['type']))
            return $this->error('操作类型不能为空');
        if ($postData['type'] == 'add') {
            DepartmentUser::save($this->request->except('type'));
        } else {
            $dpUser = DepartmentUser::where('user_id', $postData['user_id'])
                ->where('department_id', $postData['department_id'])->find();
            $dpUser->save($this->request->except('type'));
        }
        return $this->success();
    }

    public function delDepartmentUser()
    {
        $postData = $this->request->post();
        if (empty($postData['user_id'])) return $this->error('用户id不能为空');
        if (empty($postData['department_id'])) return $this->error('部门id不能为空');
        $dpUser = DepartmentUser::where('user_id', $postData['user_id'])
            ->where('department_id', $postData['department_id'])->find();
        if (empty($dpUser))
            return $this->error('没找到部门员工信息');
        $dpUser->delete();
        return $this->success();
    }

    public function addRole()
    {
        $postData = $this->request->post();
        if (empty($postData['user_id'])) return $this->error('用户id不能为空');
        if (empty($postData['group_id'])) return $this->error('角色id不能为空');
        AuthGroupAccess::save($postData);
        return $this->success();
    }

    public function delRole()
    {
        $postData = $this->request->post();
        if (empty($postData['user_id'])) return $this->error('用户id不能为空');
        if (empty($postData['group_id'])) return $this->error('角色id不能为空');
        $roleUser = AuthGroupAccess::where('uid', $postData['user_id'])
            ->where('group_id', $postData['group_id'])->find();
        $roleUser->delete();
        return $this->success();
    }
}
